package semant.firstpass;

import semant.Env;
import symbol.ClassInfo;
import symbol.Symbol;
import syntaxtree.Formal;
import syntaxtree.Identifier;
import syntaxtree.IntegerLiteral;
import syntaxtree.IntegerType;
import syntaxtree.MainClass;
import syntaxtree.MethodDecl;
import syntaxtree.Statement;
import syntaxtree.VisitorAdapter;
import util.List;

public final class MainClassHandler extends VisitorAdapter
{
	private Env e;
	
	private MainClassHandler(Env e)
	{
		super();
		
		this.e = e;
	}
	
	public static void firstPass(Env e, MainClass mc)
	{
		MainClassHandler h = new MainClassHandler(e);
		
		mc.accept(h);
	}
	
	@Override
	public void visit(MainClass node)
	{
		Symbol s = Symbol.symbol(node.className.s);
		
		ClassInfo ci = new ClassInfo(s);
		
		// este cara deveria ser vetor de string!!!!!!!!!!!!!!!
		syntaxtree.Type formalType = (syntaxtree.Type)(new syntaxtree.IntArrayType(node.mainArgName.line,
				node.mainArgName.row));
		
		List<Formal> fList = new List<Formal>(
				new Formal(node.mainArgName.line, node.mainArgName.row,
						formalType, node.mainArgName), null);
		
		List<Statement> lStatement = new List<Statement>(node.s, null);
		
		MethodDecl mainMethod = new MethodDecl(node.line, node.row,
				new IntegerType(node.line, node.row),
				new Identifier(node.line, node.row, "main"), fList,
				null, lStatement, new IntegerLiteral(0, 0,0));
		
		List<MethodDecl> lMethod = new List<MethodDecl>(mainMethod, null);
		
		MethodDeclHandler.firstPass(e, ci, lMethod);
		
		if (!e.classes.put(s, ci))
			e.err.Error(node, new Object[]{"Nome da classe redefinido.", "Símbolo: " + s});
	}
}
